package com.erps.CommonUtils;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.erps.db.DBManager;

public class JActiveTable {
	private Object[] ParmList = null;
	private String DBTableName = "";
	private String OrderBy = "";
	private ArrayList<JActiveField> Fields = new ArrayList<JActiveField>();
	private String __rowkeys = "";
	private int __rows = 1;
	private String dbKeyFields = "";
	public static Logger log = LoggerFactory.getLogger(JActiveTable.class);

	// /获取查询总行数
	public int getRowCount(String cWhereParm) {
		String cSQL = getCountSQL(cWhereParm);
		String cValue = DBManager.GetStrValue(cSQL);
		return StringEx.getInt(cValue, 0);
	}

	public void init() {

	}

	// /获取查询SQL语句
	public String getQuerySQL(String cWhereParm) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select " + getQueryField());
		sql.append(" from " + getDBTableName());

		if (!StringEx.isEmpty(cWhereParm)) {
			sql.append(" where " + cWhereParm);
		}
		if (!StringEx.isEmpty(getOrderBy())) {
			sql.append(" order by " + this.getOrderBy());
		}
		return sql.toString();
	}

	// /获取查询SQL语句
	public String getCountSQL(String cWhereParm) {
		StringBuffer sql = new StringBuffer();
		sql.append(" select count(1)");
		sql.append(" from " + getDBTableName());

		if (cWhereParm.length() > 0) {
			sql.append(" where " + cWhereParm);
		}
		return sql.toString();
	}

	public String getDBTableName() {
		return DBTableName;
	}

	public void setDBTableName(String dBTableName) {
		DBTableName = dBTableName;
	}

	public String getOrderBy() {
		return OrderBy;
	}

	public void setOrderBy(String orderBy) {
		OrderBy = orderBy;
	}

	public String getDbKeyFields() {
		return dbKeyFields;
	}

	public void setDbKeyFields(String dbKeyFields) {
		this.dbKeyFields = dbKeyFields;
	}

	public String get__rowkeys() {
		return __rowkeys;
	}

	public void set__rowkeys(String __rowkeys) {
		this.__rowkeys = __rowkeys;
	}

	public int get__rows() {
		return __rows;
	}

	public void set__rows(int __rows) {
		this.__rows = __rows;
	}

	public ArrayList<JActiveField> getFields() {
		return Fields;
	}

	public void setFields(ArrayList<JActiveField> fields) {
		Fields = fields;
	}

	public String getOrderParm() {
		if (this.OrderBy.length() > 0)
			return " order by " + this.OrderBy;
		else
			return "";
	}

	public String getWhereParm() {
		if (__rowkeys.length() == 0)
			return "";
		String[] arKeys = dbKeyFields.split(",");
		String[] arValues = __rowkeys.split(",");
		// 一次删除多个数值
		if ((arKeys.length == 1) && (arValues.length > 0)) {
			StringBuffer sb = new StringBuffer();
			String cFieldName = StringEx.getStr(arKeys[0]);
			for (int i = 0; i < arValues.length; i++) {
				String cFieldValue = StringEx.getStr(arValues[i]);
				String cRowKey = cFieldName + "='" + cFieldValue + "'";
				sb.append(sb.length() == 0 ? cRowKey : " or " + cRowKey);
			}
			return sb.toString();
		} else if (arKeys.length == arValues.length) {
			StringBuffer sb = new StringBuffer();
			for (int i = 0; i < arValues.length; i++) {
				String cFieldName = StringEx.getStr(arKeys[i]);
				String cFieldValue = StringEx.getStr(arValues[i]);
				String cRowKey = cFieldName + "='" + cFieldValue + "'";
				sb.append(sb.length() == 0 ? cRowKey : " and " + cRowKey);
			}
			return sb.toString();
		} else {
			log.error("参数传递错误！");
			return "";
		}
	}

	public String getDeleteSQL() {
		return "delete from " + this.DBTableName + " where " + this.getWhereParm();
	}

	public String getQueryField() {
		StringBuffer sb = new StringBuffer();
		for (int i = 0; i < this.Fields.size(); i++) {
			JActiveField myField = (JActiveField) Fields.get(i);
			if (sb.length() == 0)
				sb.append(myField.getFieldName());
			else
				sb.append("," + myField.getFieldName());
		}
		return sb.toString();
	}

	public JActiveTable() {

	}

	public void Clear() {
		this.Fields.clear();
	}

	public void AddField(JActiveField myField) {
		if (Fields == null) {
			Fields = new ArrayList();
		}
		Fields.add(myField);
	}

	public void setForeignInfo(String cFieldName, String cFKTable, String cFKKey, String cFKText, String cWhere) {
		JActiveField jfField = this.getFieldByName(cFieldName);

		if (jfField == null) {
			return;
		}
		jfField.setFKTABLE(cFKTable);
		jfField.setFKKEY(cFKKey);
		jfField.setFKTEXT(cFKText);
		jfField.setFKWHERE(cWhere);
	}

	public List<G_KeyValue> getDropList(JActiveField jfField) {
		String cFKTable = jfField.getFKTABLE();
		String cFKKey = jfField.getFKKEY();
		String cFKText = jfField.getFKTEXT();
		String cFKWhere = jfField.getFKWHERE();
		if ((cFKTable.length() == 0) || (cFKKey.length() == 0) || (cFKText.length() == 0)) {
			return null;
		}
		StringBuffer sql = new StringBuffer();
		sql.append("select " + cFKKey + " as keyname," + cFKText + " as keyid from " + cFKTable);
		if (cFKWhere.length() > 0) {
			sql.append(" where " + cFKWhere);
		}
		return DBManager.QueryList(sql.toString());
	}

	public String getDropDownName(String cFieldName, String cKeyValue) {
		JActiveField jfField = this.getFieldByName(cFieldName);
		if (jfField == null) {
			return "";
		}
		String oKeyValue = null;
		List<G_KeyValue> dtRows = getDropList(jfField);
		if (dtRows != null) {
			int iLength = dtRows.size();
			for (int i = 0; i < iLength; i++) {
				G_KeyValue arKey = dtRows.get(i);
				String cUserKeyValue = arKey.getKeyValue();
				String cKeyName = arKey.getKeyName();
				if (cUserKeyValue.equals(cKeyValue)) {
					// oKeyValue = cKeyValue + Constant.NAME_VALUE +
					// cKeyName;
					oKeyValue = cKeyName;
					break;
				}
			}
		}

		return oKeyValue;
	}

	public String getFieldList() {
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < this.Fields.size(); i++) {
			JActiveField myField = (JActiveField) (Fields.get(i));
			String StrFieldName = myField.getFieldName();
			if (i == 0) {
				sb.append(StrFieldName);
			} else {
				sb.append("," + StrFieldName);
			}
		}
		return sb.toString();
	}

	public JActiveField getFieldByName(String cFieldName) {
		int iLength = this.Fields.size();
		for (int i = iLength - 1; i >= 0; i--) {
			JActiveField myField = (JActiveField) (Fields.get(i));
			if (myField.getFieldName().toLowerCase().equals(cFieldName.toLowerCase())) {
				return myField;
			}
		}
		return null;
	}

	public void addFieldValue(String cFieldName, int cFieldValue) {
		addFieldValue(cFieldName, String.valueOf(cFieldValue));
	}

	public void addFieldValue(String cFieldName, Long cFieldValue) {
		addFieldValue(cFieldName, String.valueOf(cFieldValue));
	}

	public void addFieldValue(String cFieldName, String cFieldValue) {
		JActiveField jfField = this.getFieldByName(cFieldName);
		if (jfField != null) {
			jfField.setFieldValue(cFieldValue);
		} else {
			jfField = new JActiveField();
			jfField.setFieldName(cFieldName);
			jfField.setFieldValue(cFieldValue);
			AddField(jfField);
		}
	}

	public void setFieldValue(String cFieldName, String cFieldValue) {
		JActiveField jfField = this.getFieldByName(cFieldName);
		if (jfField != null) {
			jfField.setFieldValue(cFieldValue);
		}
	}

	public void setFieldValue(String cFieldName, int iValue) {
		JActiveField jfField = this.getFieldByName(cFieldName);
		if (jfField != null) {
			jfField.setFieldValue(String.valueOf(iValue));
		}
	}

	public void setFieldValue(String cFieldName, long iValue) {
		JActiveField jfField = this.getFieldByName(cFieldName);
		if (jfField != null) {
			jfField.setFieldValue(String.valueOf(iValue));
		}
	}

	public void setFieldValue(String cFieldName, double dValue) {
		JActiveField jfField = this.getFieldByName(cFieldName);
		if (jfField != null) {
			jfField.setFieldValue(String.valueOf(dValue));
		}
	}

	public void RemoveField(String cFieldName) {
		JActiveField jfField = this.getFieldByName(cFieldName);
		if (jfField != null) {
			this.Fields.remove(jfField);
		}
	}

	public void setDefaultFieldValue(String cFieldName, String cFieldValue) {
		JActiveField jfField = getFieldByName(cFieldName);
		if (jfField != null) {
			jfField.setNullAsValue(cFieldValue);
		}
	}

	public void setFieldsisKey(String cFieldList) {
		int iLength = this.Fields.size();
		String[] arFields = cFieldList.split(",");
		for (int i = 0; i < iLength; i++) {
			JActiveField myField = (JActiveField) (Fields.get(i));
			for (int k = 0; k < arFields.length; k++) {
				String StrFieldName = arFields[i];
				if (myField.getFieldName().equals(StrFieldName)) {
					Fields.get(i).setIsKey(true);
				}
			}
		}
	}

	public void setFieldsRequire(String cFieldList) {
		int iLength = this.Fields.size();
		String[] arFields = cFieldList.split(",");
		for (int i = 0; i < iLength; i++) {
			JActiveField jfField = (JActiveField) (Fields.get(i));
			for (int k = 0; k < arFields.length; k++) {
				String StrFieldName = arFields[k];
				if (jfField.getFieldName().equals(StrFieldName)) {
					jfField.setIsRequire(true);
				}
			}
		}
	}

	public String FieldValue(String StrFieldName) {
		JActiveField jfField = this.getFieldByName(StrFieldName);
		if (jfField != null)
			return jfField.getFieldValue();
		else
			return "";
	}

	public String FillDropDown(String StrFieldName) {
		JActiveField jfField = this.getFieldByName(StrFieldName);
		if (jfField == null)
			return "";

		List<G_KeyValue> dtRows = getDropList(jfField);
		if (dtRows == null)
			return "";
		String cFieldValue = StringEx.getStr(jfField.getFieldValue());
		StringBuffer dropList = new StringBuffer();
		int iLength = dtRows.size();
		for (int i = 0; i < iLength; i++) {
			G_KeyValue arKey = dtRows.get(i);
			String cKeyName = arKey.getKeyName();
			String cKeyValue = arKey.getKeyValue();
			// cKeyName = cKeyValue + Constant.NAME_VALUE + cKeyName;
			String cOptionKey = "";
			if (cKeyValue.equals(cFieldValue)) {
				cOptionKey = "<option selected value='" + cKeyValue + "'>" + cKeyName + "</option>";
			} else {
				cOptionKey = "<option value='" + cKeyValue + "'>" + cKeyName + "</option>";
			}
			dropList.append(cOptionKey);
		}
		return dropList.toString();
	}

	public String FillDropDown(String cSQL, String cFieldValue) {
		cFieldValue = StringEx.getStr(cFieldValue);
		List<G_KeyValue> dtRows = DBManager.QueryList(cSQL);
		return FillDropDown(dtRows, cFieldValue);
	}

	public String FillDropDown(List<G_KeyValue> dtRows, String cFieldValue) {
		cFieldValue = StringEx.getStr(cFieldValue);
		StringBuffer dropList = new StringBuffer();
		int iLength = dtRows.size();
		for (int i = 0; i < iLength; i++) {
			G_KeyValue arKey = dtRows.get(i);
			String cKeyName = StringEx.getStr(arKey.getKeyName());
			String cKeyValue = StringEx.getStr(arKey.getKeyValue());
			if ((cKeyName.length() == 0) || (cKeyValue.length() == 0))
				continue;
			String cOptionKey = "";
			if (cKeyValue.equals(cFieldValue)) {
				cOptionKey = "<option selected value='" + cKeyValue + "'>" + cKeyName + "</option>";
			} else {
				cOptionKey = "<option value='" + cKeyValue + "'>" + cKeyName + "</option>";
			}
			dropList.append(cOptionKey);
		}
		dropList.insert(0, "<option value=''>请选择</option>");
		return dropList.toString();
	}

	public Boolean hasKey(String[] cvs, String cValue) {
		Boolean flag = false;
		if (cvs != null) {
			for (int i = 0; i < cvs.length; i++) {
				String cFieldValue = cvs[i];
				if (cFieldValue.equals(cValue)) {
					flag = true;
					break;
				}
			}
		}
		return flag;
	}

	public String getListRadio(String StrFieldName) {
		JActiveField jfField = this.getFieldByName(StrFieldName);
		if (jfField == null)
			return "";

		String cFieldValue = jfField.getFieldValue();
		List<G_KeyValue> dtRows = getDropList(jfField);
		if (dtRows == null)
			return "";

		StringBuffer dropList = new StringBuffer();
		int iLength = dtRows.size();
		for (int i = 0; i < iLength; i++) {
			G_KeyValue arKey = dtRows.get(i);
			String cKeyName = arKey.getKeyName();
			String cKeyValue = arKey.getKeyValue();
			String cOptionKey = "";
			String cFieldName = jfField.getFieldName();
			String cCtrlID = cFieldName + "_" + String.valueOf(i);
			if (cKeyValue.equals(cFieldValue)) {
				cOptionKey = "<input type='radio' id='" + cCtrlID + "' name='" + cFieldName + "' checked value='" + cKeyValue + "'><label for='" + cCtrlID + "'>" + cKeyName + "</label>";
			} else {
				cOptionKey = "<input type='radio' id='" + cCtrlID + "' name='" + cFieldName + "' value='" + cKeyValue + "'><label for='" + cCtrlID + "'>" + cKeyName + "</label>";
			}
			dropList.append(cOptionKey);
		}
		return dropList.toString();
	}

	public String getListBox(String StrFieldName) {
		JActiveField jfField = this.getFieldByName(StrFieldName);
		if (jfField == null)
			return "";

		String cFieldValue = jfField.getFieldValue();
		List<G_KeyValue> dtRows = getDropList(jfField);
		if (dtRows == null)
			return "";
		String[] cvs = cFieldValue.split(AppConfig.ARRAY_LITE);
		StringBuffer dropList = new StringBuffer();
		int iLength = dtRows.size();
		for (int i = 0; i < iLength; i++) {
			G_KeyValue arKey = dtRows.get(i);
			String cKeyName = arKey.getKeyName();
			String cKeyValue = arKey.getKeyValue();
			String cOptionKey = "";
			String cFieldName = jfField.getFieldName();
			String cCtrlID = cFieldName + "_" + String.valueOf(i);
			if (hasKey(cvs, cKeyValue)) {
				cOptionKey = "<input type='checkbox' id='" + cCtrlID + "' name='" + cFieldName + "' checked value='" + cKeyValue + "'><label for='" + cCtrlID + "'>" + cKeyName + "</label>";
			} else {
				cOptionKey = "<input type='checkbox' id='" + cCtrlID + "' name='" + cFieldName + "' value='" + cKeyValue + "'><label for='" + cCtrlID + "'>" + cKeyName + "</label>";
			}
			dropList.append(cOptionKey);
		}
		return dropList.toString();
	}

	public ActionResult Validate() {
		ActionResult vret = null;
		int iLength = Fields.size();
		for (int j = 0; j < iLength; j++) {
			JActiveField jfField = (JActiveField) (Fields.get(j));
			String cFieldName = jfField.getFieldName();
			String cDBFDisplay = jfField.getDBFDisplay();
			String cFieldValue = jfField.getFieldValue();
			if (jfField.getIsKey()) {
				if (cFieldValue.length() == 0) {
					vret = new ActionResult(cFieldName, cDBFDisplay + "主键数据不能为空！");
					break;
				}
			}
			if (jfField.getIsRequire()) {
				if (cFieldValue.length() == 0) {
					vret = new ActionResult(cFieldName, cDBFDisplay + "数据不能为空！");
					break;
				}
			}
			if (jfField.getFieldType() == ActiveField.ftString) {
				if (cFieldValue.length() > jfField.getDBLength()) {
					vret = new ActionResult(cFieldName, cDBFDisplay + "超过指定长度！");
					break;
				}
			}
			if (jfField.getFieldType() == ActiveField.ftLong) {
				if (cFieldValue.length() > 0) {
					try {
						Long.valueOf(cFieldValue);
					} catch (Exception ex) {
						vret = new ActionResult(cFieldName, cDBFDisplay + "必须为长整形！");
						break;
					}
				}
			}
			int ifType = jfField.getFieldType();
			if (ifType == ActiveField.ftInteger) {
				if (cFieldValue.length() > 0) {
					try {
						Integer.valueOf(cFieldValue);
					} catch (Exception ex) {
						vret = new ActionResult(cFieldName, cDBFDisplay + "必须为短整形！");
						break;
					}
				}
			}
			if (ifType == ActiveField.ftFloat) {
				if (cFieldValue.length() > 0) {
					try {
						Integer.valueOf(cFieldValue);
					} catch (Exception ex) {
						vret = new ActionResult(cFieldName, cDBFDisplay + "必须为浮点数！");
						break;
					}
				}
			}
			if (ifType == ActiveField.ftDateTime) {
				if (cFieldValue.length() > 0) {
					try {
						DateUtils.StrToDateTime(cFieldValue);
					} catch (Exception ex) {
						vret = new ActionResult(cFieldName, cDBFDisplay + "必须为日期类型！");
						break;
					}
				}
			}
		}
		return vret;
	}

	public Object[] getParmList() {
		return ParmList;
	}

	public String getParmSQL() {
		ParmList = new Object[Fields.size()];
		StringBuilder sb = new StringBuilder();
		String StrKeyNames = "";
		String StrKeyValues = "";
		int idx = 0;
		if (StringEx.getStr(__rowkeys).equals("")) {
			try {
				sb.append(" insert into " + DBTableName);
				sb.append("(" + getFieldList() + ")");
				sb.append("values(");
				int iLength = Fields.size();
				for (int j = 0; j < iLength; j++) {
					JActiveField jfField = (JActiveField) (Fields.get(j));
					String cFieldName = jfField.getFieldName();
					String cFieldValue = StringEx.getStr(jfField.getFieldValue());
					int ifType = jfField.getFieldType();
					if (ifType == ActiveField.ftBoolean) {
						if (cFieldValue.equals("1")) {
							ParmList[idx] = 1;
						} else {
							ParmList[idx] = 0;
						}
					} else {
						if (cFieldValue.length() > 0) {
							if (ifType == ActiveField.ftDateTime) {
								if (Validity.IsDateTime(cFieldValue)) {
									ParmList[idx] = cFieldValue;
								} else {
									ParmList[idx] = null;
								}
							} else {
								ParmList[idx] = cFieldValue.trim();
							}
						} else {
							String cDefValue = jfField.getNullAsValue();
							if (cDefValue.length() > 0) {
								if (cDefValue.startsWith("{SQL}")) {
									cDefValue = cDefValue.substring(5);
									String cValue = DBManager.GetStrValue(cDefValue);
									if (cValue.length() > 0)
										ParmList[idx] = cFieldValue.trim();
									else
										ParmList[idx] = null;
								} else
									ParmList[idx] = cDefValue.trim();
							} else
								ParmList[idx] = null;
						}
					}
					// 是主键中的一个
					if (dbKeyFields.indexOf(cFieldName) > -1) {
						StrKeyNames = StrKeyNames.length() == 0 ? cFieldName : StrKeyNames + "," + cFieldName;
						StrKeyValues = StrKeyValues.length() == 0 ? cFieldValue : StrKeyNames + "," + cFieldValue;
					}
					sb.append(j == 0 ? "?" : ",?");
					idx++;
				}

				sb.append(")");
			} catch (Exception ex) {
				log.error(ex.getMessage());
			}
		} else {
			try {
				sb.append(" update " + this.DBTableName);
				sb.append(" set ");
				int iLength = Fields.size();
				for (int j = 0; j < iLength; j++) {
					JActiveField jfField = (JActiveField) (Fields.get(j));
					String cFieldName = jfField.getFieldName();
					String cFieldValue = StringEx.getStr(jfField.getFieldValue());
					int ifType = jfField.getFieldType();
					if (ifType == ActiveField.ftBoolean) {
						if (cFieldValue.equals("1")) {
							ParmList[idx] = 1;
						} else {
							ParmList[idx] = 0;
						}
					} else {
						if (cFieldValue.length() > 0) {
							if (ifType == ActiveField.ftDateTime) {
								if (Validity.IsDateTime(cFieldValue)) {
									ParmList[idx] = cFieldValue;
								} else {
									ParmList[idx] = null;
								}
							} else {
								ParmList[idx] = cFieldValue;
							}
						} else {
							String cDefValue = jfField.getNullAsValue();
							if (cDefValue.length() > 0) {
								if (cDefValue.startsWith("{SQL}")) {
									cDefValue = cDefValue.substring(5);
									String cValue = DBManager.GetStrValue(cDefValue);
									if (cValue.length() > 0)
										ParmList[idx] = cValue;
									else
										ParmList[idx] = null;
								} else
									ParmList[idx] = cDefValue;
							} else
								ParmList[idx] = null;
						}
					}
					// 是主键中的一个
					if (dbKeyFields.indexOf(cFieldName) > -1) {
						StrKeyNames = StrKeyNames.length() == 0 ? cFieldName : StrKeyNames + "," + cFieldName;
						StrKeyValues = StrKeyValues.length() == 0 ? cFieldValue : StrKeyValues + "," + cFieldValue;
					}

					sb.append(j == 0 ? cFieldName + "=?" : "," + cFieldName + "=?");
					idx++;
				}
				sb.append(" where " + getWhereParm());
			} catch (Exception ex) {
				log.error(ex.getMessage());
			}
		}

		return sb.toString();
	}

	public String getSQL() {
		StringBuilder sb = new StringBuilder();
		String StrKeyNames = "";
		String StrKeyValues = "";
		if (StringEx.getStr(__rowkeys).equals("")) {
			try {
				sb.append(" insert into " + DBTableName);
				sb.append("(" + getFieldList() + ")");
				sb.append("values(");
				int iLength = Fields.size();
				for (int j = 0; j < iLength; j++) {
					JActiveField jfField = (JActiveField) (Fields.get(j));
					String cFieldName = jfField.getFieldName();
					String cFieldValue = StringEx.getStr(jfField.getFieldValue());
					int ifType = jfField.getFieldType();
					if (ifType == ActiveField.ftBoolean) {
						if (cFieldValue.equals("1"))
							cFieldValue = "1";
						else
							cFieldValue = "0";
					} else {
						if (cFieldValue.length() > 0) {
							if (ifType == ActiveField.ftDateTime) {
								if (Validity.IsDateTime(cFieldValue)) {
									cFieldValue = "'" + cFieldValue + "'";
								} else {
									cFieldValue = " null ";
								}
							} else {
								cFieldValue = "'" + cFieldValue.trim() + "'";
							}
						} else {
							String cDefValue = jfField.getNullAsValue();
							if (cDefValue.length() > 0) {
								if (cDefValue.startsWith("{SQL}")) {
									cDefValue = cDefValue.substring(5);
									String cValue = DBManager.GetStrValue(cDefValue);
									if (cValue.length() > 0)
										cFieldValue = "'" + cValue + "'";
									else
										cFieldValue = "null";
								}
								cFieldValue = "'" + cDefValue + "'";
							} else
								cFieldValue = "null";
						}
					}
					// 是主键中的一个
					if (dbKeyFields.indexOf(cFieldName) > -1) {
						StrKeyNames = StrKeyNames.length() == 0 ? cFieldName : StrKeyNames + "," + cFieldName;
						StrKeyValues = StrKeyValues.length() == 0 ? cFieldValue : StrKeyNames + "," + cFieldValue;
					}
					sb.append(j == 0 ? cFieldValue : "," + cFieldValue);
				}

				sb.append(")");
			} catch (Exception ex) {
				log.error(ex.getMessage());
			}
		} else {
			try {
				sb.append(" update " + this.DBTableName);
				sb.append(" set ");
				int iLength = Fields.size();
				for (int j = 0; j < iLength; j++) {
					JActiveField jfField = (JActiveField) (Fields.get(j));
					String cFieldName = jfField.getFieldName();
					String cFieldValue = StringEx.getStr(jfField.getFieldValue());
					int ifType = jfField.getFieldType();
					if (ifType == ActiveField.ftBoolean) {
						if (cFieldValue.equals("1"))
							cFieldValue = "1";
						else
							cFieldValue = "0";
					} else {
						if (cFieldValue.length() > 0) {
							if (ifType == ActiveField.ftDateTime) {
								if (Validity.IsDateTime(cFieldValue)) {
									cFieldValue = "'" + cFieldValue + "'";
								} else {
									cFieldValue = " null ";
								}
							} else {
								cFieldValue = "'" + cFieldValue.trim() + "'";
							}
						} else {
							String cDefValue = jfField.getNullAsValue();
							if (cDefValue.length() > 0) {
								if (cDefValue.startsWith("{SQL}")) {
									cDefValue = cDefValue.substring(5);
									String cValue = DBManager.GetStrValue(cDefValue);
									if (cValue.length() > 0)
										cFieldValue = "'" + cValue + "'";
									else
										cFieldValue = "null";
								}
								cFieldValue = "'" + cDefValue + "'";
							} else
								cFieldValue = "null";
						}
					}
					// 是主键中的一个
					if (dbKeyFields.indexOf(cFieldName) > -1) {
						StrKeyNames = StrKeyNames.length() == 0 ? cFieldName : StrKeyNames + "," + cFieldName;
						StrKeyValues = StrKeyValues.length() == 0 ? cFieldValue : StrKeyValues + "," + cFieldValue;
					}

					sb.append(j == 0 ? cFieldName + "=" + cFieldValue : "," + cFieldName + "=" + cFieldValue);
				}
				sb.append(" where " + this.getWhereParm());
			} catch (Exception ex) {
				log.error(ex.getMessage());
			}
		}

		return sb.toString();
	}

}